﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

namespace MulticastTV
{
    /// <summary>
    /// Export internal (channel informations) xml format to m3u playlist.
    /// </summary>
    static class ExportToM3U
    {
        static public  void  Start(string file)
        {
            StreamWriter sw = File.CreateText(file);
            sw.WriteLine("#EXTM3U");
            int prevchan = 0;
            string prevname = "";
            DataView dw = ChannelTable.menu.Tables["Menu"].DefaultView;
            dw.Sort ="Chan";
            foreach (DataRowView dr in dw)
            {
                string ip = dr[0].ToString();
                int ch = (int)dr[1];
                string name = dr[2].ToString();
                string audio = dr[4].ToString();
                if (prevchan != ch &&  prevname != name && ch != 0 )
                {
                    sw.WriteLine("#EXTINF:0," + ch + " - " + name);
                    sw.WriteLine("udp://@" + ip);
                }
                else {
                    sw.WriteLine("#EXTINF:0," + ch + " - " +name+" ("+audio+")");
                    sw.WriteLine("udp://@" + ip);              
                }
                prevchan = ch;
                prevname = name;
            }
            sw.Close();
        }
    }
}
